package com.ruima.erp.framework.db.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.sql.rowset.CachedRowSet;


import com.ruima.erp.framework.dao.DaoUtil;
import com.ruima.erp.framework.dao.IRmSysResource;
import com.ruima.erp.framework.entity.RmSysResource;

public class RmSysResourceImpl implements IRmSysResource {

	
	public List<RmSysResource> getRmSysResource() throws SQLException{
		List<RmSysResource> list = new ArrayList<RmSysResource>();
		
		String execSql = "select * from rm_sys_resource";
		
		CachedRowSet crs = DaoUtil.SimpleQuery(execSql); 
		
		while(crs.next()){
			RmSysResource rmSysUser  = new RmSysResource();
			rmSysUser.setObjid(crs.getString("objid"));
			rmSysUser.setDisindex(crs.getString("disindex"));
			rmSysUser.setName(crs.getString("name"));
			rmSysUser.setCode(crs.getString("code"));
			rmSysUser.setDescript(crs.getString("descript"));
			rmSysUser.setCreatetime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(crs.getDate("createtime"))); 
			rmSysUser.setUpdatetime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(crs.getDate("updatetime")));

			rmSysUser.setSelfObjid(crs.getString("selfObjid"));
			rmSysUser.setCreateuserObjid(crs.getString("createuserObjid"));
			
			list.add(rmSysUser);
		}
		
		return list;
	}
	
	public RmSysResource getRmSysResourceById(String id) throws SQLException{
		String execSql = "select * from rm_sys_resource where objid = ?";
		CachedRowSet crs = DaoUtil.SimpleQuery(execSql,id); 
		RmSysResource rmSysResource  = new RmSysResource();
		if(crs.next()){
			rmSysResource.setObjid(crs.getString("objid"));
			rmSysResource.setDisindex(crs.getString("disindex"));
			rmSysResource.setName(crs.getString("name"));
			rmSysResource.setCode(crs.getString("code"));
			rmSysResource.setDescript(crs.getString("descript"));
			rmSysResource.setCreatetime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(crs.getDate("createtime"))); 
			rmSysResource.setUpdatetime(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(crs.getDate("updatetime")));

			rmSysResource.setSelfObjid(crs.getString("selfObjid"));
			rmSysResource.setCreateuserObjid(crs.getString("createuserObjid"));
		}
		return rmSysResource;
	}
	
	public RmSysResource addRmSysResource(RmSysResource rmSysResource) throws SQLException {
		String execSql = "insert into rm_sys_resource(objid,disindex,name,code,descript,createtime,updatetime," +
				"selfObjid,createuserObjid) values(?,?,?,?,?,?,?,?,?)";
		String objid = UUID.randomUUID().toString();
		Object[] params = new Object[]{
				objid,
				new Date().getTime(),
				rmSysResource.getName(),
				rmSysResource.getCode(),
				rmSysResource.getDescript(),
				new Date(),
				new Date(),
				rmSysResource.getSelfObjid(),
				rmSysResource.getCreateuserObjid()
		};
		DaoUtil.executeUpdate(execSql, params); 
		rmSysResource.setObjid(objid);
		return rmSysResource;
	}
	
	public RmSysResource editRmSysResource(RmSysResource rmSysResource)throws SQLException{
		String execSql = "update rm_sys_resource set " +
				"disindex = ? ," +
				"name = ?," +
				"code = ?," +
				"descript = ?," +
				"createtime = ?," +
				"updatetime = ?," +
				"selfObjid = ?," +
				"createuserObjid = ? " +
				" where objid = ? ";
		Object[] params = new Object[]{
				new Date().getTime(),
				rmSysResource.getName(),
				rmSysResource.getCode(),
				rmSysResource.getDescript(),
				new Date(),
				new Date(),
				rmSysResource.getSelfObjid(),
				rmSysResource.getCreateuserObjid(),
				rmSysResource.getObjid()
		};
		DaoUtil.executeUpdate(execSql, params); 
		return rmSysResource;
	}
	
	public RmSysResource deleteRmSysResource(RmSysResource rmSysResource)throws SQLException{
		String execSql = "delete from rm_sys_resource where objid = ? ";
		Object[] params = new Object[]{
				rmSysResource.getObjid()
		};
		DaoUtil.executeUpdate(execSql, params); 
		return rmSysResource;
	}
	
	public static void main(String[] args) throws SQLException {
		//System.out.println();new RmSysUserImpl().getRmSysUsers().size();
		RmSysResource rmSysResource = new RmSysResource();
		rmSysResource.setObjid("1");
		rmSysResource.setName("xxxxxxxxx");
		rmSysResource.setCode("xxxxxxxxx");
		rmSysResource.setCreateuserObjid("1");
		rmSysResource.setSelfObjid("4107031981039283948");
		
		//new RmSysUserImpl().addRmSysUser(rmSysUser);
		
		//new RmSysUserImpl().editRmSysUser(rmSysUser);
		
		new RmSysResourceImpl().deleteRmSysResource(rmSysResource);
		
		
	}
}
